package com.ponhu.ea2.goodsCenter.bms.api.controller;

import com.alibaba.excel.EasyExcel;
import com.ponhu.ea2.dto.PhInventoryDetailDto;
import com.ponhu.ea2.service.facade.PhInventoryDetailApiService;
import com.ponhu.ea2.vo.PhEditPriceLogExcelVo;
import com.ponhu.ea2.vo.PhInventoryDetailExcelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author luyu
 * @create 2021-12-15 11:49
 */
@RestController
@RequestMapping("/phInventoryDetail")
@Api(tags = "货品业务接口", value = "PhInventoryDetailController")
public class PhInventoryDetailController {

    @DubboReference(version = "1.0.0", check = false)
    PhInventoryDetailApiService phInventoryDetailApiService;

    @ApiOperation(value = "导出编辑绩效表")
    @PostMapping("/exportExcelToEdit")
    public void exportExcelToEdit(@RequestBody @Validated PhInventoryDetailDto phInventoryDetailDto,
                                  HttpServletResponse response) {

        List<PhInventoryDetailExcelVo> list = phInventoryDetailApiService.queryAll(phInventoryDetailDto);
        OutputStream output = null;
        try {
            output = response.getOutputStream();
            String fileName = "编辑绩效表";
            String sheetName = "编辑绩效表";
            // 使用swagger 会导致各种问题，请直接用浏览器或者用postman
            response.setContentType("application/msexcel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(fileName,"UTF-8") + ".xlsx");
            EasyExcel.write(output , PhInventoryDetailExcelVo.class).sheet(sheetName).doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @ApiOperation(value = "导出价格修改日志表")
    @PostMapping("/getEditPriceLog")
    public void exportExcelToPrice(@RequestBody @Validated PhInventoryDetailDto phInventoryDetailDto,
                                   HttpServletResponse response){
        List<PhEditPriceLogExcelVo> list = phInventoryDetailApiService.queryEditPriceLog(phInventoryDetailDto);

        OutputStream output = null;
        try {
            output = response.getOutputStream();
            String fileName = "价格修改日志表";
            String sheetName = "价格修改日志表";
            // 使用swagger 会导致各种问题，请直接用浏览器或者用postman
            response.setContentType("application/msexcel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(fileName,"UTF-8") + ".xlsx");
            EasyExcel.write(output , PhEditPriceLogExcelVo.class).sheet(sheetName).doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
